import java.awt.Point;
import java.util.ArrayList;

import javax.swing.JOptionPane;

public class ModelPanelGame extends AbstractModel {
	private static final int BLACK = 0;
	private static final int WHITE = 1;
	
	private Board board;
	private Integer gameMode;	// 1: Single play	2: Multiple play
	private AI ai;
	boolean isBlackTurn;
	private AvailableBlocks available;
	private Reverse reverse;
	Boolean gameover;
	Integer Turn_count;
	
	public ModelPanelGame(){
		// Do nothing
	}
	
	public void setGame(Integer mode){
		board = new Board();
		board.initiation();	
		gameMode = mode;
		if(mode == 1)
			ai = new AI();
		isBlackTurn = true;
		available = new AvailableBlocks();
		available.calc(board,BLACK);
		reverse = new Reverse();
		gameover = new Boolean(false);
		Turn_count = new Integer(1);
		firePropertyChange("gameMode",0,gameMode);
		firePropertyChange("gameover",0,gameover);
		firePropertyChange("board", 0, board);
		firePropertyChange("availableBlocks",0,available.getAvailableBlocks());
	}
	
	public void setClickedBlockCoordLeft(Point clicked){

		// Player Black Turn
		if(isBlackTurn){
			playerBlack(clicked);
			if(gameMode==1){
				CPUWhite();
				addTurn_count();
			}
		}
		// Player White Turn
		else{
			if(gameMode==2){
				playerWhite(clicked);
				addTurn_count();
			}
		}
		
	}
	
	private void playerBlack(Point clicked){
		board.setBlack(clicked);
		reverse.calc(board, BLACK, clicked);
		board.setBlack(reverse.getReverse());
		isBlackTurn = false;
		available.calc(board,WHITE);
		if(available.getAvailableBlocks().isEmpty()){		// if White has no available
			isBlackTurn = true;						  		// skip White's turn
			available.calc(board,BLACK);
			if(available.getAvailableBlocks().isEmpty())	// if Black has no available too
				setGameover();							// game over
		}
		else
			firePropertyChange("turn",(Integer)BLACK,(Integer)WHITE);
	}
	private void playerWhite(Point clicked){
		board.setWhite(clicked);
		reverse.calc(board, WHITE, clicked);
		board.setWhite(reverse.getReverse());
		isBlackTurn = true;
		available.calc(board,BLACK);
		if(available.getAvailableBlocks().isEmpty()){ 		// if Black has no available
			isBlackTurn = false;					  		// skip Black's turn
			available.calc(board,WHITE);
			if(available.getAvailableBlocks().isEmpty()) 	// if White has no available too
				setGameover();							// game over
		}
		else
			firePropertyChange("turn",(Integer)WHITE,(Integer)BLACK);
	}
	private void CPUBlack(){
	}
	private void CPUWhite(){
		Point CPUclicked = new Point(ai.getEasyAI(available.getAvailableBlocks()));
		board.setWhite(CPUclicked);
		reverse.calc(board, WHITE, CPUclicked);
		board.setWhite(reverse.getReverse());
		firePropertyChange("CPUReady",false,true);	// Tell View the CPU is ready to print
		isBlackTurn = true;
		available.calc(board,BLACK);
		if(available.getAvailableBlocks().isEmpty()){ 		// if Black has no available
			isBlackTurn = false;					  		// skip Black's turn
			available.calc(board,WHITE);
			if(available.getAvailableBlocks().isEmpty()) 	// if White has no available too
				setGameover();							// game over
		}
		else
			firePropertyChange("turn",(Integer)WHITE,(Integer)BLACK);
	}
	
	private void addTurn_count(){
		Integer old = Turn_count;
		Turn_count++;
		firePropertyChange("Turn_count",old,Turn_count);
	}
	private void setGameover(){
		gameover = true;
		firePropertyChange("gameover",0,gameover);

	}

}
